package com.xjscrm.console.chat.core.impl.conversation;

import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RConversationShieldMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatConversationShieldMsgHandler extends ChatBaseMsgHandler<RConversationShieldMsg> {

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;

    @Override
    public Class<RConversationShieldMsg> msgClass() {
        return RConversationShieldMsg.class;
    }

    @Override
    public boolean checkMsg(RConversationShieldMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getTalkSessionId()) || Objects.isNull(baseMsg.getOpen());
    }

    @Override
    public SResultMsg doHandle(RConversationShieldMsg content, ChannelHandlerContext ctx) {

        ChatTalkSession byId = chatTalkSessionService.getById(content.getMerId(), content.getTalkSessionId());
        if (Objects.isNull(byId)) {
            throw new BusinessException("会话不存在: 屏蔽失败");
        }
        chatTalkSessionStaffRelService.delSessionRel(content.getMerId(), content.getStaffId(), byId.getDeviceUniqueId(), byId.getId());
        if (content.getOpen()) {
            ChatTalkSession updateSession = new ChatTalkSession();
            updateSession.setId(byId.getId());
            updateSession.setMerId(byId.getMerId());
            updateSession.setOpenShield(1);
            updateSession.setOpenShieldTime((int) (System.currentTimeMillis() / 1000L));
            updateSession.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            chatTalkSessionService.update(updateSession);

            return SResultMsg.success(content, "会话屏蔽成功");
        } else {
            ChatTalkSession updateSession = new ChatTalkSession();
            updateSession.setId(byId.getId());
            updateSession.setMerId(byId.getMerId());
            updateSession.setOpenShield(0);
            updateSession.setOpenShieldTime(-1);
            updateSession.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
            chatTalkSessionService.update(updateSession);

            return SResultMsg.success(content, "取消会话屏蔽成功");
        }
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_conversation_shield;
    }
}
